import json
import re

import requests
import execjs

with open("140.js", "r", encoding="utf-8") as f:
    js = f.read()
ctx = execjs.compile(js)


def extract_csessionid_value(response_text):
    """
    从JSONP响应中提取csessionid
    """
    # 方法1: 使用正则表达式移除JSONP包装
    # 匹配 callback_name(json_data) 格式
    pattern = r'jsonp_\d+\((.*)\);?'
    match = re.search(pattern, response_text)

    if match:
        json_str = match.group(1)
        try:
            # 解析JSON数据
            data = json.loads(json_str)
            # 提取csessionid
            csessionid = data.get('result', {}).get('csessionid')
            value = data.get('result', {}).get('value')
            return csessionid,value
        except json.JSONDecodeError as e:
            print(f"JSON解析错误: {e}")
            return None
    else:
        print("未找到匹配的JSONP格式")
        return None
headers = {
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Pragma": "no-cache",
    "Referer": "https://www.newrank.cn/",
    "Sec-Fetch-Dest": "script",
    "Sec-Fetch-Mode": "no-cors",
    "Sec-Fetch-Site": "cross-site",
    "Sec-Fetch-Storage-Access": "active",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
url = "https://cf.aliyun.com/nocaptcha/analyze.jsonp"

n = ctx.call("get140")
print("n:::", n)
params = {
    "a": "FFFF0N00000000009594",
    "t": "FFFF0N00000000009594:1751614679479:0.46419691870140656",
    # "n": "140#DnsrIIWCzzZkFzo2mx4bAtSoYi3rdwy1tPw1kn6nSWBImWA+LQQuvRLQLndXiy3SmgC96H5A0z3teV2FjZYvGowPIFSFAxtClp1zzXXre00tvzzxxxPLOtEtzzrb22U3lp1xzFYrV2lBUFrzKID+V3hA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0dxPTsU7n2offBFQkvnjlfGc7+vaUgVR0V9SIk+sb4RocWae/HJMqgjxjHioOUBhxJBkm96duIxKzGoQ1U+ls3Ey62V+s+ojoG1Mw9Ch0f0W4xZ/y7rXv4Y+5em9xfaHDiqdUauPLP8D3qqLCJJWn46IXwCde7PZJ1BMFwYxs2PBGYvZi4seuDAOMtFzowMpNz5o7L0Cwe6cSur66kp75fM554SNAUhbARBdWvaL3epPFOeQ5KhtUc0DQ9a0ytTLufWyJUb0HNw8RIKuos8lEOSQMlIZhysDCFDij/pcWPg6qBHia4Kc6yDHGrjlexVlyvOoORI2hDC8rovjN5mxtQ9PCEOVZS3royEVDHmibd3fLzhOuk33ImBm6FZk3yGuKb3ppCEouM/IXMilEoC4nzdGFhumBu3+at3nbJ8kZhcFzz==",
    "n": n,
    "p": "{\"ncSessionID\":\"65b47dcd7fe9\",\"umidToken\":\"T2gAhlcXD7PnX7KoJC0lbWo5WzJX_lzuFkFGN5OBxiteghrtXfLNUZf36ZZsqVUhZs4=\"}",
    "scene": "nc_login",
    "asyn": "0",
    "lang": "cn",
    "v": "1099",
    "callback": "jsonp_038438408024940385"
}
response = requests.get(url, headers=headers, params=params)
print(response.text)
print(response.status_code)
sessionId,value=extract_csessionid_value(response.text)
print("sessionId:::",sessionId)
print("value:::",value)


def login(sessionId,value):

    headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
        "Content-Type": "application/x-www-form-urlencoded",
        "Origin": "https://www.newrank.cn",
        "Pragma": "no-cache",
        "Referer": "https://www.newrank.cn/user/login?displayType=login&action=feedback&source=9006&backUrl=https%3A%2F%2Fwww.newrank.cn%2F%3Fsource%3D9006%26unit%3Dbing-pinpaiweihu%26keyword%3Dxinbang1%26msclkid%3Dd007ad6d593c140e47bfc50addf4f1f9",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
        "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\""
    }
    cookies = {
        "acw_tc": "1a0c399817516146678553217e005a9612eaaeb4589f16574a4c75ae4c4e5e",
        "Hm_lvt_a19fd7224d30e3c8a6558dcb38c4beed": "1751614667",
        "Hm_lpvt_a19fd7224d30e3c8a6558dcb38c4beed": "1751614667",
        "HMACCOUNT": "EA2E0839E5E39945",
        "sajssdk_2015_cross_new_user": "1",
        "sensorsdata2015jssdkcross": "%7B%22distinct_id%22%3A%22197d45ebb2b1731-0d64270cb6534c-4c657b58-2073600-197d45ebb2c2195%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.bing.com%2F%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTk3ZDQ1ZWJiMmIxNzMxLTBkNjQyNzBjYjY1MzRjLTRjNjU3YjU4LTIwNzM2MDAtMTk3ZDQ1ZWJiMmMyMTk1In0%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%22%2C%22value%22%3A%22%22%7D%2C%22%24device_id%22%3A%22197d45ebb2b1731-0d64270cb6534c-4c657b58-2073600-197d45ebb2c2195%22%7D",
        "NR_PAGE_SOURCE": "9006",
        "NR_MAIN_SOURCE_RECORD": "{\"locationSearch\":\"?source=9006&unit=bing-pinpaiweihu&keyword=xinbang1&msclkid=d007ad6d593c140e47bfc50addf4f1f9\",\"locationHref\":\"https://www.newrank.cn/?source=9006&unit=bing-pinpaiweihu&keyword=xinbang1&msclkid=d007ad6d593c140e47bfc50addf4f1f9\",\"referrer\":\"https://www.bing.com/\",\"source\":\"9006\",\"keyword\":\"xinbang1\",\"firstReferrer\":\"\",\"firstLocation\":\"\",\"sourceHref\":\"https://www.newrank.cn/?source=9006&unit=bing-pinpaiweihu&keyword=xinbang1&msclkid=d007ad6d593c140e47bfc50addf4f1f9\"}",
        "tfstk": "gVmxK5AWOQAckKoYEx8lsJ0RM6vk-UD42jkCj5Vc5bh-dv505AvV1PGiZo46gAcRfAGLISGgm1KtwoaZo5V06lHZ9LAHxHDq3lz6tBx3_JTulk51GG1gFTwQX-sPepDi3lr6ZveA-gMqgXoLqfZ6euwTIZZ_1S98PJP5cRsbC_98a7wbhPN_NuwaBR_j1l98PRV71laj1u98Qj29676bsG3HkXW1PiJhXGn8HrFOj-sts0S3yW_4aMsQM-UAoxNRfGi-WYA6aSTFE5r0gAyx_HS4cP3jM8Z9XgZxgxi_ycAkxrHQCfrmVd_bOqc0VmgdCGM8DSai0yCvwWiqFDr7rH9KF0lm30HGChwot7MqclKBLoEbGuerjQSb64gsmrrNGIEseRsPIDmpQff39RbXeLQN7rwrPdGuqAzJvTe8tKKO7NzyU8FHeLQN7rwzeWvvBN7azL5.."
    }
    url = "https://www.newrank.cn/nr/user/login/loginByAccount"
    data = {
        "account": "18320970000",
        "adeOrMedia": "0",
        "password": "3c5f76ed7473858fe04225e2fd18cd14",
        "scene": "nc_login",
        "sessionId":sessionId,
        "sig": value,
        "state": "1",
        "token": "FFFF0N00000000009594:1751614679479:0.46419691870140656",
        "nonce": "923108498",
        "xyz": "c14ba0249a1c3520118b0e1e26d6d404"
    }
    response = requests.post(url, headers=headers, cookies=cookies, data=data)

    print("login res:::",response.text)
    print("response:::",response.status_code)

if __name__ == '__main__':
    result=login(sessionId,value)
